<?php
namespace Console\OBD\Events;

use Swoole\Server;

class GPSEvent extends Base
{
    public function initialize()
    {
        parent::initialize();
    }

    /**
     * GPS|OBD混合信息上传
     *
     * @param Server $server
     * @param        $fd
     * @param        $from_id
     * @param        $message
     *
     * @return bool
     */
    public function uploadGpsAndObdInfo(Server $server, $fd, $from_id, $message)
    {
        if (!is_array($message)) {
            return false;
        }
        $data                                 = [];
        $data['device_id']                    = $message['deviceId'];
        $data['cmd']                          = $message['cmd'];
        $cmdContent                           = str_split($message['cmdContent'], 2);
        $data['data_type']                    = $cmdContent[0];
        $data['route_id']                     = hexdec($cmdContent[1]) + hexdec($cmdContent[2]);
        $data['date']                         = $this->ToDate(
            [
                $cmdContent[3],
                $cmdContent[4],
                $cmdContent[5]
            ],
            [
                $cmdContent[6],
                $cmdContent[7],
                $cmdContent[8]
            ]
        );
        $data['lat']                          = round(intval($cmdContent[9]) + intval($cmdContent[10] . $cmdContent[11] . $cmdContent[12]) / 600000, 4);
        $data['lng']                          = round(intval($cmdContent[13] . substr($cmdContent[14], 0, 1)) + intval(substr($cmdContent[14], 1, 1) . $cmdContent[15] . $cmdContent[16] . substr($cmdContent[17], 0, 1)) / 600000, 4);
        $data['location_direction']           = $this->formatBinLocationDirection(hexdec(substr($cmdContent[17], 1, 1)));
        $data['speed']                        = hexdec($cmdContent[18]);
        $data['direction']                    = hexdec($cmdContent[19]);
        $data['gps_num']                      = hexdec($cmdContent[20]);
        $data['gsm_signal']                   = hexdec($cmdContent[21]);
        $data['mileage']                      = $cmdContent[22] . $cmdContent[23] . $cmdContent[24] . $cmdContent[25];
        $data['device_status']                = $cmdContent[26] . $cmdContent[27] . $cmdContent[28] . $cmdContent[29];
        $data['load_sum']                     = hexdec($cmdContent[30]) * 100 / 255;
        $data['coolant_temperature']          = hexdec($cmdContent[31]) - 40;
        $data['engine_speed']                 = ((hexdec($cmdContent[32]) * 256) + hexdec($cmdContent[33])) / 4;
        $data['obd_speed']                    = hexdec($cmdContent[34]);
        $data['spark_advance_angle']          = hexdec($cmdContent[35]) - 64;
        $data['map']                          = hexdec($cmdContent[36]);
        $data['ecm_voltage']                  = hexdec($cmdContent[37]) / 10;
        $data['air_inlet_temperature']        = hexdec($cmdContent[38]) - 40;
        $data['air_mass_flow']                = ((hexdec($cmdContent[39]) * 256) + hexdec($cmdContent[40])) / 100;
        $data['restrictor']                   = hexdec($cmdContent[41]) * 100 / 255;
        $data['long_ft']                      = (hexdec($cmdContent[42]) - 128) * 100 / 128;
        $data['air_fuel_ratio']               = (hexdec($cmdContent[43] * 256 + hexdec($cmdContent[44]))) * 0.0000305;
        $data['restrictor_absolute_position'] = hexdec($cmdContent[45]) * 100 / 255;
        $data['fuel_pressure']                = hexdec($cmdContent[46]) * 3;
        $data['Instent_fuel']                 = (hexdec($cmdContent[47]) * 256 + hexdec($cmdContent[48])) * 0.1;
        if ((chr(hexdec($cmdContent[49])) & chr(0x80)) == chr(0x80)) {
            $data['remain'] = ((((hexdec($cmdContent[49]) - hexdec('80')) * 256) + (hexdec($cmdContent[50]))) * 0.1) . '%';
        } else {
            $data['remain'] = (((hexdec($cmdContent[49]) * 256) + hexdec($cmdContent[50])) * 0.1) . 'L';
        }
        try {
            if ($this->db()->insertAsDict('obd_gps_mixture_info', $data)) {
                return true;
            };
        } catch (\PDOException $ex) {
            $this->logger()->error(sprintf('数据库操作失败，原因为[%s],错误文件为[%s],错误行数为[%s]', $ex->getMessage(), __FILE__, __LINE__));
        }
    }

    public function requestGpsAndObdInfo(Server $server, $fd, $from_id, $message)
    {

    }

    public function queryGpsAndObdUploadInfo(Server $server, $fd, $from_id, $message)
    {

    }

    public function setGpsAndObdUploadInfo(Server $server, $fd, $from_id, $message)
    {

    }

    public function UploadGpsLocation(Server $server, $fd, $from_id, $message)
    {
        if (!is_array($message)) {
            return false;
        }
        $data              = [];
        $data['device_id'] = $message['deviceId'];
        $data['cmd']       = $message['cmd'];
        $cmdContent        = str_split($message['cmdContent'], 2);

        $data['data_type']           = hexdec($cmdContent[0]);
        $data['route_id']            = (hexdec($cmdContent[1]) << 8) + hexdec($cmdContent[2]);
        $data['date']                = $this->ToDate([
            $cmdContent[3],
            $cmdContent[4],
            $cmdContent[5]
        ], [
                $cmdContent[6],
                $cmdContent[7],
                $cmdContent[8]
            ]
        );
        $data['lat']                 = round(intval($cmdContent[9]) + (intval($cmdContent[10] . $cmdContent[11] . $cmdContent[12]) / 600000), 4);
        $data['lng']                 = round(intval($cmdContent[13] . substr($cmdContent[14], 0, 1)) + intval(substr($cmdContent[14], 1, 1) . $cmdContent[15] . $cmdContent[16] . substr($cmdContent[17], 0, 1)) / 600000, 4);
        $data['location_ direction'] = hexdec(substr($cmdContent[17], 1, 1));
        $data['speed']               = hexdec($cmdContent[18]);
        $data['mileage']             = (hexdec($cmdContent[22]) << 24) + (hexdec($cmdContent[23] << 16)) + (hexdec($cmdContent[24]) << 8) + hexdec($cmdContent[25]);
        $data['device_status']       = $cmdContent[26] . $cmdContent[27] . $cmdContent[28] . $cmdContent[29];
        try {
            if ($this->db()->insertAsDict('obd_gps_location_info', $data)) {
                return true;
            };
        } catch (\PDOException $ex) {
            $this->logger()->error(sprintf('数据库操作失败，原因为[%s],错误文件为[%s],错误行数为[%s]', $ex->getMessage(), __FILE__, __LINE__));
        }
    }

    public function requestGpsLocation(Server $server, $fd, $from_id, $message)
    {

    }
}
